library(haven)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
categ <- read_dta(file = "ByCateg_v5.dta")

categfull <- categ %>%
  filter(!is.na(price))

Initial Data Visualizations

ggplot(data = categfull, mapping = aes(x = price, y = densprot)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Protein on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = denssodi)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Sodium on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = dens_add_sugars)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Sugar on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = densfibe)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Fiber on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

First Plotly Iteration

# Legend
leg <- list(
  bordercolor = "#E2E2E2",
  borderwidth = 0.5)

# Protein 
prot <- plot_ly(
  categfull, x = ~price, y = ~densprot,
  # Hover text:
  text = ~paste("<b>Type:</b>", categ_name, "<br><b>Description:</b>", categ_des),
  color = ~broadcateg_name, size = ~tspend, opacity = 0.8
)

prot %>%
  layout(title = "Protein",
                     xaxis = list(title = "Price/1000kcal"), yaxis = list(title = "Protein/1000kcal"),
                     margin = list(l = 123), legend = leg) 

Plotly Visualization with Dropdown Feature

#Plot with Dropdown

prot2 <- plot_ly(
  data = categfull, x = ~price, y = ~densprot, 
          color =  ~broadcateg_name, size = ~tspend,
          alpha = 0.7,
          text = ~paste0("<b>", categ_name, "</b>",  "<br><i>Description: </i>", categ_des),
          type = "scatter",
          mode = "markers"
          ) %>%
  layout(
    title = "Price v. Nutrient Density",
    xaxis = list(title = "Price/1000kcal"),
    yaxis = list(title = "y"),
    # yaxis = list(title = "Protein/1000kcal"),
       updatemenus = list(
        list(
          y = .3,
          x = 1.45,
        type = "list",
        label = 'Category',
        buttons = list(
          list(method = "restyle",
               args = list("y", list(categfull$densprot)),
               label = "Protein"),
          list(method = "restyle",
               args = list("y", list(categfull$densfibe)),
               label = "Fiber"),
          list(method = "restyle",
               args = list("y", 
                list(categfull$dens_add_sugars)),
               label = "Added Sugar"),
          list(method = "restyle",
               args = list("y", list(categfull$denssodi)),
               label = "Sodium")))))

prot2